package com.vmj.heap;

import com.vmj.rtda.Slot;

/**
 * Created by yangshoujun on 2017/8/26 下午8:44.
 */
public class Slots {

    Slot[] slots;

    public Slots(Slot[] slots) {
        this.slots = slots;
    }

    public void setInt(int index, int value) {
        Slot slot = new Slot();
        slot.setNum(value);
        slots[index] = slot;
    }

    public int getInt(int index) {
        Slot slot = slots[index];
        if(slot == null) {
            return 0;
        }
        return slots[index].getNum();
    }

    public void setFloat(int index, float value) {
        Slot slot = new Slot();
        int val = Float.floatToIntBits(value);
        slot.setNum(val);
        slots[index] = slot;
    }

    public float getFloat(int index) {
        Slot slot = slots[index];
        if(slot == null) {
            return 0;
        }
        int val = slots[index].getNum();
        return Float.intBitsToFloat(val);
    }

    public void setLong(int index, long value) {
        int i1 = (int)value;
        int i2 = (int)(value >> 32);

        Slot slot1 = new Slot();
        slot1.setNum(i1);
        slots[index] = slot1;

        Slot slot2 = new Slot();
        slot2.setNum(i2);
        slots[++index] = slot2;
    }

    public long getLong(int index) {
        Slot slot = slots[index];
        if(slot == null) {
            return 0;
        }
        int i1 = getInt(index);
        int i2 = getInt(++index);

        long l1 = (long)((i2 & 0x000000ffffffffL) << 32);
        long l2 = i1 & 0x00000000ffffffffL;
        return l1 | l2;
    }

    public void setDouble(int index, double value) {
        long l = Double.doubleToLongBits(value);
        setLong(index, l);
    }

    public double getDouble(int index) {
        Slot slot = slots[index];
        if(slot == null) {
            return 0;
        }
        long l = getLong(index);
        double d = Double.longBitsToDouble(l);
        return d;
    }

    public void setRef(int index, VMObject object) {
        Slot slot = new Slot();
        slot.setRef(object);
        slots[index] = slot;
    }

    public VMObject getRef(int index) {
        Slot slot = slots[index];
        if(slot == null) {
            return null;
        }
        return (VMObject) slot.ref;
    }
}
